package cn.edu.ncepu.junit.learn;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import static org.junit.jupiter.api.Assertions.*;

/**
 *
 * 2025-11-05
 *
 * @author 醒过来摸鱼
 */
@ExtendWith(MockitoExtension.class)
class StaticServiceTest {


    @InjectMocks
    private StaticService staticService;

    private static MockedStatic<HelloUtil> helloUtil;

    @Test
    void callStatic() {
        helloUtil = Mockito.mockStatic(HelloUtil.class);
        helloUtil.when(()->HelloUtil.hello()).thenReturn("hello");
        String s = staticService.callStatic();
        assertEquals(s, "hello");
    }


    @Test
    void parallel() throws ExecutionException, InterruptedException {
        helloUtil = Mockito.mockStatic(HelloUtil.class);
        helloUtil.when(()->HelloUtil.hello()).thenReturn("hello");

        String s = staticService.callStatic();
        assertEquals(s, "hello");
        FutureTask<String> task = new FutureTask<>(() -> staticService.callStatic());
        new Thread(task).start();
        String ret = task.get();
        assertEquals(ret, "hello");
    }

   @AfterAll
   static void close() {
       helloUtil.close();
   }
}